<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属园区" prop="parkNum">
        <el-select v-model="queryParams.parkNum" placeholder="请选择园区">
          <el-option
            v-for="item in parkList"
            :key="item.id"
            :label="item.parkNum"
            :value="item.parkNum"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="仪表类型" prop="meterType">-->
        <!--<el-select v-model="queryParams.meterType" placeholder="请选择仪表类型" clearable size="small">-->
          <!--<el-option-->
            <!--v-for="item in meterTypeList"-->
            <!--:key="item.dictValue"-->
            <!--:label="item.dictLabel"-->
            <!--:value="item.dictValue"-->
          <!--&gt;-->
          <!--</el-option>-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="物资分类" prop="deviceType">-->
        <!--<el-select v-model="queryParams.deviceType" placeholder="请选择物资分类" clearable size="small">-->
          <!--<el-option label="请选择字典生成" value="" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="仪表名称" prop="meterName">-->
        <!--<el-input-->
          <!--v-model="queryParams.meterName"-->
          <!--placeholder="请输入仪表名称"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="仪表编码" prop="meterNum">
        <el-input
          v-model="queryParams.meterNum"
          placeholder="请输入仪表编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="仪表型号" prop="meterModel">-->
        <!--<el-input-->
          <!--v-model="queryParams.meterModel"-->
          <!--placeholder="请输入仪表型号"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="设备状态" prop="state">-->
        <!--<el-select clearable-->
          <!--v-model="queryParams.state"-->
          <!--placeholder="请选择设备状态">-->
          <!--<el-option-->
            <!--v-for="item in workState"-->
            <!--:key="item.dictValue"-->
            <!--:label="item.dictLabel"-->
            <!--:value="item.dictValue"-->
          <!--&gt;-->
          <!--</el-option>-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['energy:nyMeterAccount1:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['energy:nyMeterAccount1:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['energy:nyMeterAccount1:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energy:nyMeterAccount1:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="nyMeterAccount1List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="Id" align="center" prop="id" />-->
      <el-table-column label="仪表编码" align="center" prop="meterNum" />
      <el-table-column label="仪表名称" align="center" prop="meterName" />
      <el-table-column label="仪表类型" align="center" prop="meterType" />
      <el-table-column label="仪表型号" align="center" prop="meterModel" />
      <el-table-column label="生产厂商" align="center" prop="supplier" />
      <el-table-column label="倍率" align="center" prop="magnification" />
      <el-table-column label="初始读数" align="center" prop="initialValue" />
      <el-table-column label="所属园区" align="center" prop="parkNum" />
      <el-table-column label="物资分类" align="center" prop="deviceType" />
      <el-table-column label="设备状态" align="center" prop="state" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energy:nyMeterAccount1:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energy:nyMeterAccount1:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仪台账资产对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仪表编码" prop="meterNum" v-if="form.id">
          <el-input v-model="form.meterNum" placeholder="请输入仪表编码" disabled />
        </el-form-item>
        <!--<el-form-item label="仪表类型" prop="meterType">-->
        <!--<el-select clearable v-model="form.meterType" placeholder="请选择仪表类型">-->
        <!--<el-option-->
        <!--v-for="item in meterTypeList"-->
        <!--:key="item.dictValue"-->
        <!--:label="item.dictLabel"-->
        <!--:value="item.dictValue"-->
        <!--&gt;-->
        <!--</el-option>-->
        <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="仪表名称" prop="meterName">
          <el-input v-model="form.meterName" placeholder="请输入仪表名称" />
        </el-form-item>
        <!--<el-form-item label="物资分类" prop="deviceType">-->
          <!--<el-select v-model="form.deviceType" placeholder="请选择物资分类">-->
            <!--<el-option label="请选择字典生成" value="" />-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="设备分类" v-if="!form.id">
          <treeselect v-model="form.sysBmId" :options="sysBmTypeOptions" :normalizer="bmNormalizer"
                      @select="genCode" placeholder="选择设备分类" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="仪表型号" prop="meterModel">
          <el-input v-model="form.meterModel" placeholder="请输入仪表型号" />
        </el-form-item>
        <el-form-item label="生产厂商" prop="supplier">
          <el-input v-model="form.supplier" placeholder="请输入生产厂商" />
        </el-form-item>
        <el-form-item label="所属园区" prop="parkNum">
          <el-select v-model="form.parkNum" placeholder="请选择园区" @change="selectPark(form.parkNum)">
            <el-option
              v-for="item in parkList"
              :key="item.id"
              :label="item.parkNum"
              :value="item.parkNum"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备状态" prop="state">
        <el-select
          clearable
          v-model="form.state"
          placeholder="请选择设备状态"
        >
          <el-option
            v-for="item in workState"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="倍率" prop="magnification">
          <el-input v-model="form.magnification" placeholder="请输入倍率" />
        </el-form-item>
        <el-form-item label="初始读数" prop="initialValue">
          <el-input v-model="form.initialValue" placeholder="请输入初始读数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNyMeterAccount1, getNyMeterAccount1, delNyMeterAccount1, addNyMeterAccount1, updateNyMeterAccount1 } from "@/api/smartParks/energy/nyMeterAccount1";
import { listData } from "@/api/system/dict/data"; //字典接口
import {listBuiPark} from "@/api/parkAssets/enterCommunity";
import * as request from '@/utils/request';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "NyMeterAccount1",
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仪台账资产表格数据
      nyMeterAccount1List: [],
      meterTypeList: [],
      workState: [],
      parkList: [],
      sysBmTypeOptions:this.$store.state.permission.sysBmTree,
      disabled:false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceType: null,
        meterName: null,
        meterNum: null,
        meterModel: null,
        supplier: null,
        state: null,
        magnification: null,
        initialValue: null,
        meterType: '水表'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        meterType: [
          {
            required: true,
            message: "仪表类型不能为空",
            trigger: "change",
          },
        ],
        //meterName:[{required: true,message: "名称不能为空", trigger: "change",}],
        parkNum:[{required: true,message: "园区不能为空", trigger: "change",}],
        // magnification:[{required: true,message: "仪表倍率不能为空", trigger: "change",}],
        initialValue:[{required: true,message: "仪表初始值不能为空", trigger: "change",}],
      }
    };
  },
  created() {
    this.getList();
    this.getListFirst();
  },
  methods: {
    /** 查询仪台账资产列表 */
    getList() {
      this.loading = true;
      listNyMeterAccount1(this.queryParams).then(response => {
        this.nyMeterAccount1List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListFirst() {
      listData({ dictType: "meter_type" }).then((response) => {
        this.meterTypeList = response.rows;
      });
      listData({ dictType: "device_work_state" }).then((response) => {
        this.workState = response.rows;
      });
      // listQyEnterprise().then((response) => {
      //   this.qyEnterpriseList = response.rows;
      // });
      // listWySupplier().then((response) => {
      //   this.supplierList = response.rows;
      // })
      //查询园区列表，赋值给parkList;
      listBuiPark().then((response) =>{
        this.parkList = response.rows;
      })
    },
    genCode(item){
      this.form.sysBmId=item.bmId
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        meterType: '水表',
        deviceType: null,
        meterName: null,
        meterNum: null,
        meterModel: null,
        supplier: null,
        state: null,
        magnification: null,
        initialValue: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加水表资产";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNyMeterAccount1(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仪台账资产";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNyMeterAccount1(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNyMeterAccount1(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除仪台账资产编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delNyMeterAccount1(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      request.downloadCheckMeter1('energy/nyMeterAccount1/export', {
        ...this.queryParams
      }, `energy_nyMeterAccount1.xlsx`)
    }
  }
};
</script>
